package com.yqg.education.controller;


import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.yqg.education.common.Result;
import com.yqg.education.common.interceptor.SysLog;
import com.yqg.education.entity.OperateRecord;
import com.yqg.education.service.IOperateRecordService;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.List;

/**
 * OperateRecordController 前端控制器
 * @author zxf
 * @since 2025-03-01
 */
@RestController
@RequestMapping("/operate-record")
public class OperateRecordController {

    @Resource
    private IOperateRecordService operateRecordService;

    /**
    *  新增或者更新
    */
    @PostMapping("/edit")
//    @SysLog("新增或者修改操作记录")
    public Result save(@RequestBody OperateRecord operateRecord) {
        operateRecord.setIsDelete(0);
        return Result.success(operateRecordService.saveOrUpdate(operateRecord));
    }
    /**
    *  删除
    */
    @GetMapping("delete/{id}")
//    @SysLog("删除单个操作记录")
    public Result delete(@PathVariable Integer id) {
        OperateRecord operateRecord =operateRecordService.getById(id);
        operateRecord.setIsDelete(1);
        return Result.success(operateRecordService.saveOrUpdate(operateRecord));
    }
    /**
     *  删除列表
     */
    @PostMapping("/deleteByIdList")
//    @SysLog("删除多个操作记录")
    public Result deleteByIdList(@RequestBody List<Long> idList) {
        List<OperateRecord> operateRecords = operateRecordService.listByIds(idList);
        for (OperateRecord recordOperate : operateRecords) {
            recordOperate.setIsDelete(1);
        }
        return Result.success(operateRecordService.saveOrUpdateBatch(operateRecords));
    }
    /**
    *  查询所有
    */
    @GetMapping("/all")
    public Result findAll() {
        QueryWrapper<OperateRecord> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("is_delete", "0");
       List<OperateRecord> list=  operateRecordService.list(queryWrapper);
        return Result.success(list);
    }
    /**
    *  根据id查询
    */
    @GetMapping("/{id}")
    public Result findOne(@PathVariable Integer id) {
        return Result.success(operateRecordService.getById(id));
    }
    /**
    *  分页查询
    */
    @GetMapping("/page")
    public Result findPage(@RequestParam Integer pageNum,
                                @RequestParam Integer pageSize) {
        QueryWrapper<OperateRecord> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("is_delete", "0");
        queryWrapper.orderByDesc("id");
        return Result.success(operateRecordService.page(new Page<>(pageNum, pageSize), queryWrapper));
    }

}

